package serverletGrade;

import Server.GradeServer;
import Server.StudentServer;
import user.Grade;
import user.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "findGradeServlet",urlPatterns = "/findgrade")
public class findGradeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
            Grade grade = null;
            GradeServer gradeServer = new GradeServer();
            if (null!=request.getParameter("num")) {
                String num = request.getParameter("num");
                grade = gradeServer.findGradeServer(num);
            }
            if (null!=request.getParameter("name")) {
                String name = request.getParameter("name");
                grade = gradeServer.findGradeByNameServer(name);
            }
            List<Grade> grades=new ArrayList<>();
            grades.add(grade);
            request.getSession().setAttribute("list", grades);
            response.sendRedirect("Grade.jsp");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
